package com.shark.example.controllers
{
	import com.shark.example.events.LoginEvent;
	import com.shark.example.services.UserService;
	import com.shark.example.vos.User;
	
	import flash.events.IEventDispatcher;
	
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	import org.swizframework.utils.services.ServiceHelper;

	public class LoginController
	{
		[Dispatcher]
		public var dispatcher:IEventDispatcher;
		
		[Inject]
		public var userService:UserService;
		
		[Inject]
		public var serviceHelper:ServiceHelper;
		
		[Bindable]
		public var user:User;
		
		
		public function LoginController()
		{
			this.user = new User();
			user.name = "Gus";
			user.pass = "gabriel";
		}
		
		[EventHandler ( event="LoginEvent.LOGIN_REQUEST" ,properties="user")]
		public function loginHandler(user:User):void{
			serviceHelper.executeServiceCall(userService.loginUser(user), handleLoginResult, handleLoginFault);
		}
		
		public function handleLoginResult(event: ResultEvent):void{
			var login_success:LoginEvent = new LoginEvent(LoginEvent.LOGIN_SUCCESS);
			trace(event);
			dispatcher.dispatchEvent(login_success);
		}

		public function handleLoginFault(event: FaultEvent):void{
			var login_fault:LoginEvent = new LoginEvent(LoginEvent.LOGIN_FAULT);
			trace(event);
			dispatcher.dispatchEvent(login_fault);
		}
	}
}